﻿using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;

namespace Wzh_CheckAB
{
    public static class StringBuilderTool
    {
        [ThreadStatic]
        private static StringBuilder s_CachedStringBuilder = null;

        #region  格式化字符Format
        /// <summary> 获取格式化字符串 </summary>
        public static string Format(string format, object arg0)
        {
            if (format == null) return null;
         
            StringBuilderClear();
            s_CachedStringBuilder.AppendFormat(format, arg0);
            return s_CachedStringBuilder.ToString();
        }
        public static string Format(string format, object arg0, object arg1)
        {
            if (format == null) return null;
       
            StringBuilderClear();
            s_CachedStringBuilder.AppendFormat(format, arg0, arg1);
            return s_CachedStringBuilder.ToString();
        }
        public static string Format(string format, object arg0, object arg1, object arg2)
        {
            if (format == null) return null;
         
            StringBuilderClear();
            s_CachedStringBuilder.AppendFormat(format, arg0, arg1, arg2);
            return s_CachedStringBuilder.ToString();
        }
        public static string Format(string format, params object[] args)
        {
            if (format == null || args == null) return null;
          
            StringBuilderClear();         
            s_CachedStringBuilder.AppendFormat(format, args);
            return s_CachedStringBuilder.ToString();
        }
        #endregion


        #region 连接字符 Append
        /// <summary> 连接字符</summary>
        public static string Append(string format, string arg0)
        {
            if (format == null) return null;

            StringBuilderClear();
            s_CachedStringBuilder.Append(format);
            s_CachedStringBuilder.Append(arg0);
            return s_CachedStringBuilder.ToString();
        }
        /// <summary> 连接字符</summary>
        public static string Append(params string[] format)
        {
            if (format == null || format.Length == 0) return null;

            StringBuilderClear();
            for (int i = 0; i < format.Length; i++)
                s_CachedStringBuilder.Append(format[i]);

            return s_CachedStringBuilder.ToString();
        }
        public static string Append(string format, string arg0, string arg1, object arg2)
        {
            if (format == null) return null;

            StringBuilderClear();
            s_CachedStringBuilder.Append(format);
            s_CachedStringBuilder.Append(arg0);
            s_CachedStringBuilder.Append(arg1);
            s_CachedStringBuilder.Append(arg2);
            return s_CachedStringBuilder.ToString();
        }
        public static string Append(string format, string arg0, string arg1)
        {
            if (format == null) return null;

            StringBuilderClear();
            s_CachedStringBuilder.Append(format);
            s_CachedStringBuilder.Append(arg0);
            s_CachedStringBuilder.Append(arg1);
            return s_CachedStringBuilder.ToString();
        }
        /// <summary> 连接字符 isToLine是否换行</summary>
        public static string Append(string format,bool isToLine, params string[] args)
        {
            if (format == null || args == null) return null;

            StringBuilderClear();
            s_CachedStringBuilder.Append(format);
            foreach (var item in args)
            {
                s_CachedStringBuilder.Append(item);
                if (isToLine) s_CachedStringBuilder.Append("\n");
            }
         
            return s_CachedStringBuilder.ToString();
        }
        /// <summary>List<int> 转String</summary>
        public static string Append(List<int> listInt, char cha)
        {
            if (listInt == null || listInt.Count == 0) return null;

            StringBuilderClear();
            for (int i = 0; i < listInt.Count - 1; i++)
            {
                s_CachedStringBuilder.Append(listInt[i]);
                s_CachedStringBuilder.Append(cha);
            }
            s_CachedStringBuilder.Append(listInt[listInt.Count - 1]);
            return s_CachedStringBuilder.ToString();
        }
        /// <summary>List<int> 转String</summary>
        public static string Append(List<string> listStr, char cha)
        {
            if (listStr == null || listStr.Count == 0) return null;

            StringBuilderClear();
            for (int i = 0; i < listStr.Count - 1; i++)
            {
                s_CachedStringBuilder.Append(listStr[i]);
                s_CachedStringBuilder.Append(cha);
            }
            s_CachedStringBuilder.Append(listStr[listStr.Count - 1]);
            return s_CachedStringBuilder.ToString();
        }
        /// <summary>int[] 转String</summary>
        public static string Append(int[] listArr, char cha)
        {
            if (listArr == null || listArr.Length == 0) return null;

            StringBuilderClear();
            for (int i = 0; i < listArr.Length - 1; i++)
            {
                s_CachedStringBuilder.Append(listArr[i]);
                s_CachedStringBuilder.Append(cha);
            }
            s_CachedStringBuilder.Append(listArr[listArr.Length - 1]);
            return s_CachedStringBuilder.ToString();
        }
        /// <summary>int[] 转String</summary>
        public static string Append(float[] listArr, char cha)
        {
            if (listArr == null || listArr.Length == 0) return null;

            StringBuilderClear();
            for (int i = 0; i < listArr.Length - 1; i++)
            {
                s_CachedStringBuilder.Append(listArr[i]);
                s_CachedStringBuilder.Append(cha);
            }
            s_CachedStringBuilder.Append(listArr[listArr.Length - 1]);
            return s_CachedStringBuilder.ToString();
        }
        /// <summary>Dictionary<int,int> 转String</summary>
        public static string Append(Dictionary<int,int> listArr, char id_numSprit, char itemSprit)
        {
            if (listArr == null || listArr.Count == 0) return null;

            StringBuilderClear();
            int num = 0;
            foreach (var item in listArr)
            {
                num++;
                s_CachedStringBuilder.Append(item.Key);
                s_CachedStringBuilder.Append(id_numSprit);
                s_CachedStringBuilder.Append(item.Value);
                if (num< listArr.Count) s_CachedStringBuilder.Append(itemSprit);
            }
            return s_CachedStringBuilder.ToString();
        }


        #endregion

        #region string to object
        /// <summary>string to int</summary>
        public static int StringToInt(string str, int defValue)
        {
            str = StringRemoveEmpty(str);
            if (string.IsNullOrEmpty(str)) return defValue;
            if (int.TryParse(str, out int _stringToIntV)) return _stringToIntV;
            return defValue;
        }
        /// <summary>string to int 前缀 如：100021_xxxx </summary>
        public static int StringToInt_Prdfix(string str, char cha, int defValue)
        {
            var strArray = StringToStrArray(str, cha);
            if (strArray == null || strArray.Length == 0) return defValue;
            return StringToInt(strArray[0], defValue);
        }
        /// <summary>string to int 后缀 如：xxxx_100021 </summary>
        public static int StringToInt_Suffix(string str, char cha, int defValue)
        {
            var strArray = StringToStrArray(str, cha);
            if (strArray == null || strArray.Length == 0) return defValue;
            return StringToInt(strArray[strArray.Length - 1], defValue);
        }
        /// <summary>string to long</summary>
        public static long StringToLong(string str, long defValue)
        {
            str = StringRemoveEmpty(str);
            if (string.IsNullOrEmpty(str)) return defValue;
            if (long.TryParse(str, out long _stringToLongV)) return _stringToLongV;
            return defValue;
        }
        /// <summary>string to float</summary>
        public static float StringToFloat(string str, float defValue)
        {
            str = StringRemoveEmpty(str);
            if (string.IsNullOrEmpty(str)) return defValue;
            if (float.TryParse(str, out float _stringToFloatV)) return _stringToFloatV;
            return defValue;
        }
        /// <summary>string to float</summary>
        public static Vector3 StringToVector3(string str, char cha)
        {
            var strArray = StringToFloatArray(str, cha);
            if (strArray == null || strArray.Length < 3) return Vector3.zero;

            return new Vector3(strArray[0], strArray[1], strArray[2]);
        }
        public static Vector3Int StringToVector3Int(string str, char cha)
        {
            var strArray = StringToIntArray(str, cha);
            if (strArray == null || strArray.Length < 3) return Vector3Int.zero;

            return new Vector3Int(strArray[0], strArray[1], strArray[2]);
        }
        /// <summary>去除 空格 换行 </summary>
        public static string StringRemoveEmpty(string answer)
        {
            if (string.IsNullOrEmpty(answer)) return null;
            return System.Text.RegularExpressions.Regex.Replace(answer, @"\s", "");
        }

        #endregion

        #region string to object[]
        /// <summary>string to string[]</summary>
        public static string[] StringToStrArray(string str, char cha)
        {
            str = StringRemoveEmpty(str);
            if (string.IsNullOrEmpty(str)) return null;
            return str.Split(cha);
        }
        /// <summary>string to int[]</summary>
        public static int[] StringToIntArray(string str, char cha, int defValue = 0)
        {
            var strArray = StringToStrArray(str, cha);
            if (strArray == null || strArray.Length == 0) return null;

            int[] resArray = new int[strArray.Length];
            for (int i = 0; i < strArray.Length; i++)
                resArray[i] = StringToInt(strArray[i], defValue);
            return resArray;
        }
 
        /// <summary>string to float[]</summary>
        public static float[] StringToFloatArray(string str, char cha, float defValue = 0)
        {
            var strArray = StringToStrArray(str, cha);
            if (strArray == null || strArray.Length == 0) return null;

            float[] resArray = new float[strArray.Length];
            for (int i = 0; i < strArray.Length; i++)
                resArray[i] = StringToFloat(strArray[i], defValue);
            return resArray;
        }
        /// <summary>string to float[]</summary>
        public static long[] StringToLongArray(string str, char cha, long defValue = 0)
        {
            var strArray = StringToStrArray(str, cha);
            if (strArray == null || strArray.Length == 0) return null;

            long[] resArray = new long[strArray.Length];
            for (int i = 0; i < strArray.Length; i++)
                resArray[i] = StringToLong(strArray[i], defValue);
            return resArray;
        }

        /// <summary>string To List<Vector3></summary>
        public static Vector3[] StringToVector3Array(string str, char arrayCha, char listCha)
        {
            var strArray = StringToStrArray(str, listCha);
            if (strArray == null || strArray.Length == 0) return null;
            Vector3[] resList = new Vector3[strArray.Length];
            for (int i = 0; i < strArray.Length; i++)
                resList[i] = StringToVector3(strArray[i], arrayCha);
            return resList;
        }
        #endregion

        #region string to list<object>
        /// <summary>string to list<string></summary>
        public static List<string> StringToStrList(string str, char cha)
        {
            var strArray = StringToStrArray(str, cha);
            if (strArray == null || strArray.Length == 0) return null;
            List<string> resArray = new List<string>();
            resArray.AddRange(strArray);
            return resArray;
        }
        /// <summary>string to list<int></summary>
        public static List<int> StringToIntList(string str, char cha, int defValue = 0)
        {
            var strArray = StringToStrArray(str, cha);
            if (strArray == null || strArray.Length == 0) return null;
            List<int> resArray = new List<int>();
            for (int i = 0; i < strArray.Length; i++)
                resArray.Add(StringToInt(strArray[i], defValue));
            return resArray;
        }
        /// <summary>string to list<long></summary>
        public static List<long> StringToLongList(string str, char cha, long defValue = 0)
        {
            var strArray = StringToStrArray(str, cha);
            if (strArray == null || strArray.Length == 0) return null;
            List<long> resArray = new List<long>();
            for (int i = 0; i < strArray.Length; i++)
                resArray.Add(StringToLong(strArray[i], defValue));
            return resArray;
        }
        /// <summary>string to list<float></summary>
        public static List<float> StringToFloatList(string str, char cha, float defValue = 0)
        {
            var strArray = StringToStrArray(str, cha);
            if (strArray == null || strArray.Length == 0) return null;
            List<float> resArray = new List<float>();
            for (int i = 0; i < strArray.Length; i++)
                resArray.Add(StringToFloat(strArray[i], defValue));
            return resArray;
        }
        /// <summary>string To List<Vector3></summary>
        public static List<Vector3> StringToVector3List(string str, char arrayCha, char listCha)
        {
            var strArray = StringToStrArray(str, listCha);
            if (strArray == null || strArray.Length == 0) return null;
            List<Vector3> resList = new List<Vector3>();
            foreach (var strItem in strArray)
                resList.Add(StringToVector3(strItem, arrayCha));
            return resList;
        }
        /// <summary>字符串转数组list 1_2|2_4|3_6|4_8|5_10</summary>
        public static List<int[]> StringToIntArrayList(string str, List<int[]> defList = null, char arrayCha = '_', char listCha = '|')
        {
            var strArray = StringToStrArray(str, listCha);
            if (strArray == null || strArray.Length == 0) return defList;
            List<int[]> resList = defList == null ? new List<int[]>() : defList;
            int[] tpIntArray = null;
            foreach (var strItem in strArray)
            {
                tpIntArray = StringToIntArray(strItem, arrayCha, 0);
                if (strArray == null || strArray.Length == 0) continue;
                resList.Add(tpIntArray);
            }
            return resList;
        }
        /// <summary>字符串转数组list 1_2;2_4;3_6;4_8;5_10</summary>
        public static List<string[]> StringToStringArrayList(string str, char arrayCha, char listCha, List<string[]> defList = null)
        {
            var strArray = StringToStrArray(str, listCha);
            if (strArray == null || strArray.Length == 0) return defList;
            List<string[]> resList = defList == null ? new List<string[]>() : defList;
            string[] tpIntArray = null;
            foreach (var strItem in strArray)
            {
                tpIntArray = StringToStrArray(strItem, arrayCha);
                if (strArray == null || strArray.Length == 0) continue;
                resList.Add(tpIntArray);
            }
            return resList;
        }
        #endregion
        private static void StringBuilderClear()
        {
            if (s_CachedStringBuilder == null)
                s_CachedStringBuilder = new StringBuilder(1024);
            s_CachedStringBuilder.Length = 0;
        }
    }

}
